package com.groupbaby.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.groupbaby.ydd.common.ResultData;

import com.groupbaby.ydd.dto.ArticleNameDto;
import com.groupbaby.ydd.entity.YddArticle;
import com.groupbaby.ydd.entity.YddCate;

import com.groupbaby.ydd.entity.YddTag;
import com.groupbaby.ydd.service.IYddArticleService;
import com.groupbaby.ydd.service.IYddCateService;
import com.groupbaby.ydd.utils.PageBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Zsea
 * @since 2022-10-11
 */
@Slf4j
@RestController
@RequestMapping("/ydd_article")
public class YddArticleController {
    @Autowired
    private IYddArticleService iYddArticleService;
    @Autowired
    private IYddCateService iYddCateService;

    /*
     * /ydd_article/list*/
    @GetMapping("/list")
    public ResultData list(Long start_time, Long end_time, String title, String cateid, int page, int limit) {
        //记录数据
        log.info("start_time" + start_time, "end_time" + end_time, "title" + title, "cateid" + cateid, "page" + page, "limit" + limit);

        Page<YddArticle> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(start_time != null, YddArticle::getCreateDate, start_time)
                .le(end_time != null, YddArticle::getCreateDate, end_time);
        queryWrapper.like(StringUtils.isNotEmpty(title) ,YddArticle::getTitle, title);
        queryWrapper.eq(StringUtils.isNotEmpty(cateid), YddArticle::getCateid, cateid);


        //1查询数据
        iYddArticleService.page(pageInfo, queryWrapper);
        //2.创建新的分页对象
        PageBean<ArticleNameDto> dtoPage = new PageBean<>();

        dtoPage.setCurrentPage(pageInfo.getCurrent());
        dtoPage.setPageSize(pageInfo.getSize());
        dtoPage.setTotalPages(pageInfo.getPages());
        dtoPage.setCount(pageInfo.getTotal());

        //4.获取基本分页查询的集合数据
        List<YddArticle> records = pageInfo.getRecords();
        ArrayList<ArticleNameDto> articleNameDto = new ArrayList<>();
        //5.遍历集合

        records.forEach(article -> {
            // 拷贝
            ArticleNameDto dto = new ArticleNameDto();
            BeanUtils.copyProperties(article, dto);
            //查询分类名称

            YddCate cate = iYddCateService.getById(article.getCateid());
            dto.setCatename(cate.getCatename());
            //放入集合中
            articleNameDto.add(dto);
        });
        dtoPage.setData(articleNameDto);//注入集合
        return ResultData.ok("查询成功", dtoPage);
    }


    /*GET /ydd_article/del*/
    @GetMapping("/del")
    public ResultData del(@RequestParam String id) {
        log.info("根据id查询要删除的标签信息..." + id);
        YddArticle youArticle = iYddArticleService.getById(id);
        iYddArticleService.removeById(id);
        return ResultData.ok("删除成功", youArticle);
    }

    /*POST /ydd_article/add*/
    @PostMapping("/add")
    public ResultData add(@RequestBody YddArticle youArticle) {
        log.info("要添加的文章信息..." + youArticle.toString());

        iYddArticleService.save(youArticle);
        return ResultData.ok("添加成功", youArticle);
    }

    /*POST 标签修改  POST /ydd_tag/update*/
    @PostMapping("/update")
    public ResultData update(@RequestBody YddArticle youArticle) {
        log.info("要修改的文章信息..." + youArticle.toString());

        iYddArticleService.updateById(youArticle);
        return ResultData.ok("修改成功", youArticle);
    }

    /*文章详情 GET /ydd_article/rowInfo*/
    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestParam String id) {
        log.info("根据id查询文章信息...");
        YddArticle youArticle = iYddArticleService.getById(id);
        if (youArticle != null) {
            return ResultData.ok("查询成功", youArticle);
        }
        return ResultData.error("没有查询到对应文章信息");
    }
    /*
     * GET 文章列表-全部-*/
    @GetMapping("/all")
    public ResultData all() {
        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        List<YddArticle> list = iYddArticleService.list(queryWrapper);
        return ResultData.ok("成功", list);
    }

}
